package week3rd;


public class LinkedQueue<T> implements QueueADT<T>
{
    private int count;
    private LinearNode<T> head, tail;

    public LinkedQueue()
    {
        count = 0;
        head = tail = null;
    }

    public void enqueue(T element)
    {
        LinearNode<T> node = new LinearNode<T>(element);

        if (isEmpty())
            head = node;
        else
            tail.setNext(node);

        tail = node;
        count++;
    }


    public T dequeue() throws EmptyCollectionException
    {
        if (isEmpty())
            throw new EmptyCollectionException("queue");

        T result = head.getElement();
        head = head.getNext();
        count--;

        if (isEmpty())
            tail = null;

        return result;
    }


    public T first() throws EmptyCollectionException
    {
        if (isEmpty()) {
            throw new EmptyCollectionException("empty");
        }
        T result = head.getElement();

        return result;
    }

    public boolean isEmpty()
    {
        boolean b ;
        if (size() == 0){
            b = true;
        }
        else
        {
            b = false;
        }
        return b;
    }

    public int size()
    {
        return count;
    }

    public String toString()
    {
        LinearNode node;
        node = head;
        String str = "";
        while (node != null) {
            str += (node.getElement() + " ");
            node = node.getNext();
        }

        return str;
    }

    public void chadui(T element)
    {
        LinearNode<T> node = new LinearNode<>(element);
        if (isEmpty())
            head = node;
        else {
            node.setNext(head);
            head.setPrevious(node);
            head = head.getPrevious();

        }
        count++;
    }
}

